package com.goldcn.user.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.util.IdGen;
import com.goldcn.enums.ShopTypeEnum;
import com.goldcn.interceptor.SystemContext;
import com.goldcn.user.dao.CompanyDecorationMapper;
import com.goldcn.user.dao.CompanyDecorationPageMapper;
import com.goldcn.user.dao.CompanyImgauthMapper;
import com.goldcn.user.dao.CompanyInfoMapper;
import com.goldcn.user.dto.CompanyDecorationDescDto;
import com.goldcn.user.dto.CompanyDecorationSelectDto;
import com.goldcn.user.dto.DecorationAndPageViewDto;
import com.goldcn.user.model.CompanyDecoration;
import com.goldcn.user.model.CompanyDecorationPage;
import com.goldcn.user.model.CompanyImgauth;
import com.goldcn.user.model.CompanyInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Tim Peng
 * mail: lmn_022@163.com
 * Date: 2017/10/23
 * Time: 10:36
 * To change this template use File | Settings | File Templates.
 */
@Service
@Transactional
public class CompanyDecorationService {

    @Autowired
    CompanyInfoMapper companyInfoMapper;

    @Autowired
    CompanyDecorationMapper companyDecorationMapper;

    @Autowired
    CompanyDecorationPageService companyDecorationPageService;

    @Autowired
    CompanyImgauthMapper companyImgauthMapper;


    @Autowired
    CompanyDecorationPageMapper companyDecorationPageMapper;


    public PageInfo selectAll(int pageNum, int pageSize, CompanyDecorationSelectDto dto) {
        dto.setCompanyID(SystemContext.getCompanyID());
        PageHelper.startPage(pageNum, pageSize);
        List<CompanyDecoration> companyInfoList = companyDecorationMapper.selectAll(dto.getNameOrPhone(), SystemContext.getCompanyID());
        PageInfo<CompanyDecoration> page = new PageInfo(companyInfoList);
        return page;
    }


    public Map<String, Object> selectDecorationAndPage(Long supplierID) {
        Map<String, Object> result = new HashMap();

        CompanyDecoration companyDecoration = companyDecorationMapper.selectByTargetID(supplierID, SystemContext.getCompanyID());
        List<CompanyDecorationPage> list = companyDecorationPageService.selectDecorationPage(companyDecoration.getDecorateID());

        //result.put("companyInfo", companyInfoMapper.selectByPrimaryKey(supplierID));
        result.put("decoration", companyDecoration);
        result.put("decorationPage", list);

        return result;
    }

    public Object delDecorationPage(Long pageID) {
        return companyDecorationPageMapper.deleteByPrimaryKey(pageID);
    }

    public Object selectView(Long supplierID) {
        DecorationAndPageViewDto dto = new DecorationAndPageViewDto();

        CompanyDecoration companyDecoration = companyDecorationMapper.selectByTargetID(supplierID, SystemContext.getCompanyID());
        List<CompanyDecorationPage> list = companyDecorationPageService.selectDecorationPage(companyDecoration.getDecorateID());

        dto.setSupplierID(supplierID);
        dto.setDecorateID(companyDecoration.getDecorateID());
        dto.setBrannerImg(null == list || list.size() == 0 ? null : JSONObject.fromObject(JSONArray.fromObject(list.get(0).getSectionList()).get(0)).get("imgUrl").toString());
        dto.setImg(companyDecoration.getListImage());
        dto.setTitle(companyDecoration.getTitle());
        dto.setShopDesc(companyDecoration.getShopDesc());

        return dto;
    }

    public Object updateShopDesc(CompanyDecorationDescDto descDto) {
        CompanyDecoration companyDecoration = companyDecorationMapper.selectByPrimaryKey(descDto.getDecorateID());
        if (null == companyDecoration) {
            throw new ServiceException("店铺信息不存在", RestConst.ErrorCode.FORM_FAIL);
        }
        CompanyDecoration model = new CompanyDecoration();
        model.setShopDesc(descDto.getShopDesc());
        model.setLastUpdateDate(System.currentTimeMillis());
        model.setDecorateID(companyDecoration.getDecorateID());
        return companyDecorationMapper.updateByPrimaryKeySelective(model);
    }

    public Long insert(CompanyInfo companyInfo) {
        CompanyImgauth companyImgauth = companyImgauthMapper.selectBanner(companyInfo.getCompanyInfoID());
        CompanyDecoration companyDecoration = new CompanyDecoration();
        companyDecoration.setTitle(companyInfo.getShortName());
        companyDecoration.setShopType(ShopTypeEnum.SETTLED.getCode());
        companyDecoration.setListImage(null == companyImgauth ? null : companyImgauth.getImgUrl());
        companyDecoration.setDecorateID(IdGen.get().nextId());
        companyDecoration.setTargetID(companyInfo.getCompanyInfoID());
        companyDecoration.setCompanyID(companyInfo.getCompanyID());
        //if (StringUtil.isNotEmpty(companyInfo.getIntroduction())) {
            companyDecoration.setShopDesc(companyInfo.getIntroduction());
        //}


        companyDecorationMapper.insertSelective(companyDecoration);
        return companyDecoration.getDecorateID();
    }

    public Long update(CompanyInfo companyInfo) {
        CompanyDecoration companyDecoration = companyDecorationMapper.selectByTargetID(companyInfo.getCompanyInfoID(), SystemContext.getCompanyID());
        if (null == companyDecoration) {
            throw new ServiceException("修改供应商店铺信息失败", RestConst.ErrorCode.FORM_FAIL);
        }
        CompanyImgauth companyImgauth = companyImgauthMapper.selectBanner(companyInfo.getCompanyInfoID());
        CompanyDecoration model = new CompanyDecoration();
        model.setTitle(companyInfo.getShortName());
        model.setListImage(null == companyImgauth ? null : companyImgauth.getImgUrl());
        model.setDecorateID(companyDecoration.getDecorateID());
        //if (StringUtil.isNotEmpty(companyInfo.getIntroduction())) {
            companyDecoration.setShopDesc(companyInfo.getIntroduction());
        //}
        companyDecorationMapper.updateByPrimaryKeySelective(model);
        return companyDecoration.getDecorateID();
    }


}
